package com.css.zfzx.fwsjcj.modules.taskmanage.personfootprint.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.fwsjcj.modules.taskmanage.personfootprint.entity.FwPersonFootprintEntity;
import com.css.zfzx.fwsjcj.modules.taskmanage.personfootprint.service.FootPrintService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/fw/foot")
@Slf4j
public class FootPrintController {
    @Autowired
    FootPrintService footPrintService;

    /**
     * 保存足迹
     * @param fwPersonFootprintEntity
     * @return
     */
    @PostMapping("saveFoot")
    public RestResponse save(@RequestBody FwPersonFootprintEntity fwPersonFootprintEntity){
        RestResponse restResponse=null;
        JSONObject jsonObject=new JSONObject();
        try {
            footPrintService.saveFootPrint(fwPersonFootprintEntity);
            jsonObject.put("message","保存成功");
            restResponse=RestResponse.succeed(jsonObject);
        }catch (Exception e){
            String errormessage="保存失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 获取所有轨迹
     * @param request
     * @return
     */
    @GetMapping("allFoot")
    public RestResponse findAllFoot(HttpServletRequest request){
        RestResponse restResponse=null;
        try {
            List<FwPersonFootprintEntity> allFoot = footPrintService.findAllFoot(request);
            restResponse=RestResponse.succeed(allFoot);
        }catch (Exception e){
            String errormessage="保存失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 查看个人足迹
     * @param taskId
     * @param userId
     * @return
     */
    @GetMapping("userFoot")
    public RestResponse findByTaskIdAndUserId(String taskId,String userId,String start,String end){
        RestResponse restResponse=null;
        try {
            List<FwPersonFootprintEntity> byUserId = footPrintService.findByTaskIdAndUserIdAndTime(taskId, userId,start,end);
            restResponse=RestResponse.succeed(byUserId);
        }catch (Exception e){
            String errormessage="查询失败";
            log.error(errormessage,e);
            restResponse=RestResponse.fail(errormessage);
        }
        return restResponse;
    }
}
